/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import database_beans.Domaci;
import database_beans.LabVezbe;
import database_beans.Materijali;
import database_beans.Obavestenja;
import database_beans.Predmet;
import database_beans.Rokovi;
import database_beans.Spiskovi;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import server.Log;
import server.Util;

/**
 *
 * @author stefan
 */
@ManagedBean
@SessionScoped
public class PredmetInfo implements Serializable {

    @ManagedProperty("#{userBean}")
    private UserBean user;

    private Predmet predmet;

    private ArrayList<Predmet> predmetList;

    private ArrayList<Materijali> predavanja;

    private ArrayList<Materijali> vezbe;

    private ArrayList<Rokovi> rokovi;

    private ArrayList<LabVezbe> labVezbe;

    private ArrayList<Domaci> domaciList;

    private ArrayList<Obavestenja> obavestenjaList;

    private ArrayList<Spiskovi> spiskoviList;

    private String selectedLabVezba;

    private String selectedDomaci;

    private LabVezbe labVezba;

    private Domaci domaci;

    private boolean hasDomaci = false;

    private boolean hasLabVezba = false;

    private String[] tipMaterijala = {"", "Predavanja", "Vezbe"};

    /**
     * Creates a new instance of PredmetInfo
     */
    public PredmetInfo() {
    }

    /* @PostConstruct
     public void init() {
        
     Zaposleni z = user.getZaposleni();

     predmetList = toList(z);
     noviMaterijal = new Materijali();
     noviRok = new Rokovi();
     predavanja = new ArrayList<>();
     vezbe = new ArrayList<>();
     labVezba = new LabVezbe();
     domaci = new Domaci();
     }

     public Predmet loadPredmet() {
     HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExteranalContext().getSession(false);
     Predmet p = (Predmet) session.getAtribute("StudentPredmet");
        
     Hibernate.initialize(p.getLabVezbes());
     Hibernate.initialize(p.getDomacis());
     Hibernate.initialize(p.getMaterijalis());
     Hibernate.initialize(p.getRokovis());
       
     return p;
     }*/
    public Predmet getPredmet() {
        return predmet;
    }

    public void setPredmet(Predmet predmet) {
        this.predmet = predmet;
    }

    public UserBean getUser() {
        return user;
    }

    public void setUser(UserBean user) {
        this.user = user;
    }

    public ArrayList<Predmet> getPredmetList() {
        return predmetList;
    }

    public void setPredmetList(ArrayList<Predmet> predmetList) {
        this.predmetList = predmetList;
    }

    public ArrayList<Materijali> getPredavanja() {
        return predavanja;
    }

    public void setPredavanja(ArrayList<Materijali> predavanja) {
        this.predavanja = predavanja;
    }

    public ArrayList<Materijali> getVezbe() {
        return vezbe;
    }

    public void setVezbe(ArrayList<Materijali> vezbe) {
        this.vezbe = vezbe;
    }

    public ArrayList<Rokovi> getRokovi() {
        return rokovi;
    }

    public void setRokovi(ArrayList<Rokovi> rokovi) {
        this.rokovi = rokovi;
    }

    public ArrayList<LabVezbe> getLabVezbe() {
        return labVezbe;
    }

    public void setLabVezbe(ArrayList<LabVezbe> labVezbe) {
        this.labVezbe = labVezbe;
    }

    public ArrayList<Domaci> getDomaciList() {
        return domaciList;
    }

    public void setDomaciList(ArrayList<Domaci> domaciList) {
        this.domaciList = domaciList;
    }

    public ArrayList<Obavestenja> getObavestenjaList() {
        return obavestenjaList;
    }

    public void setObavestenjaList(ArrayList<Obavestenja> obavestenjaList) {
        this.obavestenjaList = obavestenjaList;
    }

    public ArrayList<Spiskovi> getSpiskoviList() {
        return spiskoviList;
    }

    public void setSpiskoviList(ArrayList<Spiskovi> spiskoviList) {
        this.spiskoviList = spiskoviList;
    }

    public String getSelectedLabVezba() {
        return selectedLabVezba;
    }

    public void setSelectedLabVezba(String selectedLabVezba) {
        this.selectedLabVezba = selectedLabVezba;
    }

    public String getSelectedDomaci() {
        return selectedDomaci;
    }

    public void setSelectedDomaci(String selectedDomaci) {
        this.selectedDomaci = selectedDomaci;
    }

    public LabVezbe getLabVezba() {
        return labVezba;
    }

    public void setLabVezba(LabVezbe labVezba) {
        this.labVezba = labVezba;
    }

    public Domaci getDomaci() {
        return domaci;
    }

    public void setDomaci(Domaci domaci) {
        this.domaci = domaci;
    }

    public boolean isHasDomaci() {
        return hasDomaci;
    }

    public void setHasDomaci(boolean hasDomaci) {
        this.hasDomaci = hasDomaci;
    }

    public boolean isHasLabVezba() {
        return hasLabVezba;
    }

    public void setHasLabVezba(boolean hasLabVezba) {
        this.hasLabVezba = hasLabVezba;
    }

    public String[] getTipMaterijala() {
        return tipMaterijala;
    }

    public void setTipMaterijala(String[] tipMaterijala) {
        this.tipMaterijala = tipMaterijala;
    }
    

    public String loadPredmet(String sifra) {

        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        Criteria predmetCriteria = session.createCriteria(Predmet.class);
        predmet = null;
        try {
            predmetCriteria.add(Restrictions.eq("sifra", sifra));
            predmet = (Predmet) predmetCriteria.list().get(0);
            if (predmet != null) {
                loadMaterijali(predmet);
                rokovi = new ArrayList<>(predmet.getRokovis());
                labVezbe = new ArrayList<>(predmet.getLabVezbes());
                domaciList = new ArrayList<>(predmet.getDomacis());
                obavestenjaList = new ArrayList<>(predmet.getObavestenjas());
                spiskoviList = new ArrayList<>(predmet.getSpiskovis());
            }
            t.commit();
            Log.i("Loadujem podatke predmeta sa sifrom " + sifra);
            return "predmet?faces-redirect=true";
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Materijal nije snimljen"));
            return "";
        }
    }

    private void loadMaterijali(Predmet p) {
        predavanja = new ArrayList<>();
        vezbe = new ArrayList<>();
        ArrayList<Materijali> list = new ArrayList<>(p.getMaterijalis());
        while (!list.isEmpty()) {
            Materijali m = list.remove(0);
            if (m.getTip().equals(tipMaterijala[1])) {
                predavanja.add(m);
            } else {
                vezbe.add(m);
            }
        }
    }
}
